import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            name: 'login',
            meta: { keepAlive: false },
            component: () => import('@/views/login/index.vue')
        },
        {
            path: '/index',
            name: 'index',
            redirect: '/home',
            component: () => import('@/views/index.vue'),
            children: [
                {
                    path: '/home',
                    name: 'home',
                    meta: { keepAlive: false },
                    component: () => import('@/views/home/index.vue')
                },
                {
                    path: '/touch',
                    name: 'touch',
                    meta: { keepAlive: false },
                    component: () => import('@/views/touch/index.vue')
                },
                {
                    path: '/system',
                    name: 'system',
                    redirect: '/system/user',
                    component: () => import('@/views/system/index.vue'),
                    children: [
                        {
                            path: '/system/user',
                            name: 'user',
                            meta: { keepAlive: false },
                            component: () => import('@/views/system/user/index.vue')
                        },
                        {
                            path: '/system/role',
                            name: 'role',
                            meta: { keepAlive: false },
                            component: () => import('@/views/system/role/index.vue')
                        },
                        {
                            path: '/system/menu',
                            name: 'menu',
                            meta: { keepAlive: false },
                            component: () => import('@/views/system/menu/index.vue')
                        }
                    ]
                },
                {
                    path: '/uploadImages',
                    name: 'uploadImages',
                    meta: { keepAlive: false },
                    component: () => import('@/views/uploadImages/index.vue')
                },
                {
                    path: '/routeNest',
                    name: 'routeNest',
                    redirect: '/routeNest/table',
                    component: () => import('@/views/routeNest/index.vue'),
                    children: [
                        {
                            path: '/routeNest/table',
                            name: 'table',
                            redirect: '/routeNest/table/pages',
                            component: () => import('@/views/routeNest/table/index.vue'),
                            children: [
                                {
                                    path: '/routeNest/table/pages',
                                    name: 'pages',
                                    redirect: '/routeNest/table/pages/checkbox',
                                    component: () => import('@/views/routeNest/table/pages/index.vue'),
                                    children: [
                                        {
                                            path: '/routeNest/table/pages/checkbox',
                                            name: 'checkbox',
                                            meta: { keepAlive: false },
                                            component: () => import('@/views/routeNest/table/pages/checkbox/index.vue')
                                        },
                                        {
                                            path: '/routeNest/table/pages/radio',
                                            name: 'radio',
                                            meta: { keepAlive: false },
                                            component: () => import('@/views/routeNest/table/pages/radio/index.vue')
                                        }
                                    ]
                                },
                                {
                                    path: '/routeNest/table/amountMax',
                                    name: 'amountMax',
                                    meta: { keepAlive: false },
                                    component: () => import('@/views/routeNest/table/amountMax/index.vue')
                                },
                                {
                                    path: '/routeNest/table/amount',
                                    name: 'amount',
                                    meta: { keepAlive: false },
                                    component: () => import('@/views/routeNest/table/amount/index.vue')
                                }
                            ]
                        },
                        {
                            path: '/routeNest/other',
                            name: 'other',
                            meta: { keepAlive: false },
                            component: () => import('@/views/routeNest/other/index.vue')
                        }
                    ]
                },
                {
                    path: '/excel',
                    name: 'excel',
                    redirect: '/excel/upload',
                    component: () => import('@/views/excel/index.vue'),
                    children: [
                        {
                            path: '/excel/upload',
                            name: 'upload',
                            meta: { keepAlive: false },
                            component: () => import('@/views/excel/upload/index.vue')
                        },
                        {
                            path: '/excel/export',
                            name: 'export',
                            meta: { keepAlive: false },
                            component: () => import('@/views/excel/export/index.vue')
                        },
                        {
                            path: '/excel/exportMergeHeader',
                            name: 'exportMergeHeader',
                            meta: { keepAlive: false },
                            component: () => import('@/views/excel/export-merge-header/index.vue')
                        }
                    ]
                },
                {
                    path: '/pdf',
                    name: 'pdf',
                    redirect: '/pdf/export',
                    component: () => import('@/views/pdf/index.vue'),
                    children: [
                        {
                            path: '/pdf/export',
                            name: 'pdfExport',
                            meta: { keepAlive: false },
                            component: () => import('@/views/pdf/export/index.vue')
                        },
                        {
                            path: '/pdf/print',
                            name: 'pdfPrint',
                            meta: { keepAlive: false },
                            component: () => import('@/views/pdf/print/index.vue')
                        },
                        {
                            path: '/pdf/blankPrint',
                            name: 'blankPrint',
                            meta: { keepAlive: false },
                            component: () => import('@/views/pdf/blankPrint/index.vue')
                        }
                    ]
                }
            ]
        },
        {
            path: '/print',
            name: 'print',
            meta: { keepAlive: false },
            component: () => import('@/components/print/index.vue')
        }
    ]
})

// 路由拦截器
router.beforeEach((to: any, from: any, next: any) => {
    if (to.fullPath === '/login') {
        sessionStorage.clear()
    }
    if (!sessionStorage.getItem('treeData') && to.fullPath !== '/login') {
        next({
            path: '/login',
            replace: true
        })
    } else {
        next()
    }
})

export default router
